package com.zmcc.clients;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.zmcc.entity.*;
import com.zmcc.entity.common.CommonRequest;
import com.zmcc.entity.common.CommonResponse;
import com.zmcc.sender.DefaultSyncClient;
import com.zmcc.sender.ISyncClient;
import com.zmcc.service.ISmsSendRequest;
import com.zmcc.service.impl.SmsSendRequestCommericial;
import com.zmcc.utils.WrapperUtils;

import java.util.List;

public class SmsSendClient {

    private final ISyncClient syncClient;

    private final ISmsSendRequest requestWrapper;

    private final String appkey;

    private final String appsecret;

    private final String mode;

    public SmsSendClient(Builder builder) {
        this.appkey = builder.appkey;
        this.appsecret = builder.appsecret;
        this.mode = builder.mode;
        this.syncClient = DefaultSyncClient.getInstance();
        this.requestWrapper = SmsSendRequestCommericial.getInstance();
    }

    public static class Builder {
        private String appkey;
        private String appsecret;
        private String mode;

        public Builder() {

        }

        public SmsSendClient.Builder appkey(String appkey) {
            this.appkey = appkey;
            return this;
        }

        public SmsSendClient.Builder appsecret(String appsecret) {
            this.appsecret = appsecret;
            return this;
        }

        public SmsSendClient.Builder mode(String mode) {
            this.mode = mode;
            return this;
        }

        public SmsSendClient build() {
            return new SmsSendClient(this);
        }
    }

    public SmsSendResult smsSend(SmsSendTemplate template) {
        CommonRequest request = requestWrapper.getSmsSendRequest(template);
        request.setHeaderParams(WrapperUtils.authHeaderToMap(WrapperUtils.authHeader(appkey, appsecret)));
        CommonResponse response =  this.syncClient.fetch(request);
        return new Gson().fromJson(response.getData(), SmsSendResult.class);
    }

    public SmsSendResult smsDeliveryStatus(String messageId,String date,String san) {
        CommonRequest request = requestWrapper.getSmsDeliveryStatusRequest(messageId,date,san);
        request.setHeaderParams(WrapperUtils.authHeaderToMap(WrapperUtils.authHeader(appkey, appsecret)));
        CommonResponse response =  this.syncClient.fetch(request);
        return new Gson().fromJson(response.getData(), SmsSendResult.class);
    }

    public void smsSubscribe(String smsURL, String san, String flg) {
        CommonRequest request = requestWrapper.getSmsSubscribeRequest(smsURL,san,flg);
        request.setHeaderParams(WrapperUtils.authHeaderToMap(WrapperUtils.authHeader(appkey, appsecret)));
        this.syncClient.fetch(request);
    }

    public List<SignatureOperateResult> smsAddSignnature(SmsSignatureTemplate template) {
        CommonRequest request = requestWrapper.getSmsAddSignnatureRequest(template);
        request.setHeaderParams(WrapperUtils.authHeaderToMap(WrapperUtils.authHeader(appkey, appsecret)));
        CommonResponse response =  this.syncClient.fetch(request);
        return new Gson().fromJson(response.getData(), new TypeToken<List<SignatureOperateResult>>(){}.getType());
    }

    public SmsSignatureResult smsQuerySignnature(List<String> signatureNames) {
        CommonRequest request = requestWrapper.getSmsQuerySignnatureRequest(signatureNames);
        request.setHeaderParams(WrapperUtils.authHeaderToMap(WrapperUtils.authHeader(appkey, appsecret)));
        CommonResponse response =  this.syncClient.fetch(request);
        return new Gson().fromJson(response.getData(), SmsSignatureResult.class);
    }

    public SmsSignatureResult smsDelSignnature(List<SignatureElement> signatureList) {
        CommonRequest request = requestWrapper.getSmsDelSignnatureRequest(signatureList);
        request.setHeaderParams(WrapperUtils.authHeaderToMap(WrapperUtils.authHeader(appkey, appsecret)));
        CommonResponse response =  this.syncClient.fetch(request);
        return new Gson().fromJson(response.getData(), SmsSignatureResult.class);
    }

    public SmsTemplateResult smsAddTemplate(SmsTemplate template) {
        CommonRequest request = requestWrapper.getSmsAddTemplateRequest(template);
        request.setHeaderParams(WrapperUtils.authHeaderToMap(WrapperUtils.authHeader(appkey, appsecret)));
        CommonResponse response =  this.syncClient.fetch(request);
        return new Gson().fromJson(response.getData(), SmsTemplateResult.class);
    }

    public SmsTemplateResult smsDelTemplate(List<Integer> templateIds) {
        CommonRequest request = requestWrapper.getSmsDelTemplateRequest(templateIds);
        request.setHeaderParams(WrapperUtils.authHeaderToMap(WrapperUtils.authHeader(appkey, appsecret)));
        CommonResponse response =  this.syncClient.fetch(request);
        return new Gson().fromJson(response.getData(), SmsTemplateResult.class);
    }

    public SmsTemplateResult smsQueryTemplate(List<String> templateIds) {
        CommonRequest request = requestWrapper.getSmsQueryTemplateRequest(templateIds);
        request.setHeaderParams(WrapperUtils.authHeaderToMap(WrapperUtils.authHeader(appkey, appsecret)));
        CommonResponse response =  this.syncClient.fetch(request);
        return new Gson().fromJson(response.getData(), SmsTemplateResult.class);
    }
}
